import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
  timeout: 3000,
})
// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem('token')
    const userId = sessionStorage.getItem('userId')
    config.headers[`Authorization`] = 'Bearer ' + token
    config.headers['Userid'] = userId // 拿用户id可能报错
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

// 登录接口
export function Login(loginBody) {
  return instance.request({
    url: '/login',
    method: 'POST',
    data: loginBody,
  })
}

// 查询航班信息列表
export function SelectFlights(FlightDTO) {

  console.log('Flight:',FlightDTO)
  return instance.request({
    url: '/select/flight',
    method: 'POST',
    data: FlightDTO,
  })
}

// 订票接口
export function BookOrder(data) {
  return instance.request({
    url: '/order/buy',
    method: 'POST',
    data: data,
  })
}
// 候补接口
export function WaitOrder(data) {
  return instance.request({
    url: '/order/wait',
    method: 'POST',
    data: data,
  })
}
// 获取个人订单列表
export function SelectOwnOrder(userId) {
  return instance.request({
    url: '/select/order',
    method: 'GET',
    params: {
      userId,
    },
  })
}
// 获取用户名
export function GetUserName(userId) {
  return instance.request({
    url: '/info',
    method: 'GET',
    params: {
      userId
    },
  })
}
// 导入excel航班信息
export function ExcelUpload(file) {
  const formData = new FormData();
  formData.append('file', file);
  return instance.request({
    url: '/manage/import',
    method: 'POST',
    data: formData,
  })
}
// 导出excel航班信息
export function ExcelDownload(FlightDTO) {
  return instance.request({
    url: '/manage/export',
    method: 'POST',
    data: FlightDTO,
    responseType: 'blob',
  })
}
// 退出登录
export function LoginOut() {
  return instance.request({
    url: '/logout',
    method: 'POST',
  })
}
// 退订
export function CancelOrder(orderId) {
  return instance.request({
    url: '/order/cancel',
    method: 'GET',
    params: {
      orderId,
    },
  })
}

// console.log(instance)
